﻿using System.Collections.Generic;
using System.Drawing.Printing;
using System.Management;

namespace TSites.Service.Common.Utils
{
	public class PrinterUtility
	{
		public static List<string> GetPrinterNamesOnLocal()
		{
			List<string> printerNames = new List<string>();
			foreach (string printer in PrinterSettings.InstalledPrinters)
			{
				printerNames.Add(printer);
			}

			return printerNames;
		}

		public static List<string> GetPrinterNamesOnLAN()
		{
			List<string> printerNames = new List<string>();

			string strQuery = "SELECT * FROM Win32_Printer";
			ObjectQuery objectQuery = new ObjectQuery(strQuery);
			ManagementObjectSearcher query = new ManagementObjectSearcher(objectQuery);
			ManagementObjectCollection queryCollection = query.Get();

			foreach (ManagementObject managementObject in queryCollection)
			{
				PropertyDataCollection propertyDataCollection = managementObject.Properties;
				foreach (PropertyData pd in propertyDataCollection)
				{
					if ((bool)managementObject["Default"] || (bool)managementObject["Network"])
					{
						if (managementObject[pd.Name] != null)
						{
							printerNames.Add(managementObject[pd.Name].ToString());
						}
					}
				}
				//if ((bool)managementObject["Default"]) // DEFAULT PRINTER
				//{
				//    MessageBox.Show(managementObject["Name"].ToString());
				//    Console.WriteLine(managementObject["Name"]);
				//    Console.WriteLine(managementObject["Location"]);
				//}
			}

			return printerNames;
		}
	}
}
